import { showToast } from "utils/index";
const app = getApp();
Page({
  data: {
    userInfo: null,
    showNickNamePopup: false,
    currEnv: "",
    envs: [
      { name: "dev", text: "开发" },
      { name: "test", text: "测试" },
      { name: "prod", text: "生产" },
    ],
    showEnvBtns: false,
  },

  onLoad(options) {
    this.setData({
      currEnv: wx.getStorageSync("env") || app.globalData.env,
      showEnvBtns: wx.getAccountInfoSync().miniProgram.envVersion !== "release",
    });
  },
  onShow() {
    this.setData({
      userInfo: app.globalData.userInfo,
    });
  },
  onShareAppMessage() {},
  //退出登录
  onLoginOut() {
    wx.showModal({
      title: "温馨提示",
      content: "确定要退出登录吗？",
      success: (res) => {
        if (res.confirm) {
          app.globalData.userInfo = null;
          wx.removeStorageSync("token");
          wx.removeStorageSync("userInfo");
          wx.navigateBack();
        }
      },
    });
  },
  openNickNamePopup() {
    this.setData({
      showNickNamePopup: true,
    });
  },
  //修改头像
  updateAvatar() {
    wx.chooseMedia({
      count: 1,
      success() {
        showToast("选择图片成功", "success");
      },
      fail() {
        showToast("选择图片失败");
      },
    });
  },
  onChangeEnv(e) {
    const { env } = e.currentTarget.dataset;
    this.setData({
      currEnv: env,
    });
    wx.setStorageSync("env", env);
    showToast("切换成功，请重启小程序");
  },
});
